package com.leetcode.algorithm.y22.m08.w2;

import java.util.Random;

/**
 * 384. 打乱数组
 * 
 * https://leetcode.cn/problems/shuffle-an-array/
 * 
 * @author jie.deng
 *
 */
class Question0384Solution01 {

	int[] nums;
	int n;
	Random random = new Random();

	public Question0384Solution01(int[] _nums) {
		nums = _nums;
		n = nums.length;
	}

	public int[] reset() {
		return nums;
	}

	public int[] shuffle() {
		int[] ans = nums.clone();
		for (int i = 0; i < n; i++) {
			swap(ans, i, i + random.nextInt(n - i));
		}
		return ans;
	}

	void swap(int[] arr, int i, int j) {
		int c = arr[i];
		arr[i] = arr[j];
		arr[j] = c;
	}

}